/**
 * 
 */
package com.neusoft.cmsprojects.action;


import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.JSONException;
import org.apache.struts2.json.JSONUtil;

import com.dreamtags.model.Page;
import com.neusoft.cmsprojects.model.CArticlesort;
import com.neusoft.cmsprojects.model.TbArticle;
import com.neusoft.cmsprojects.service.ArticleManagementService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * index:	
 * @author guanhaimin
 * date:2014年12月20日下午7:20:13
 */
public class ArticleManagementAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	@Resource(name = "ArticleManagementService")
	private ArticleManagementService articleManagementService;
	private String opt;
	private CArticlesort carticlesort;
	private TbArticle tbarticle;
	private int id;
	private int currentPage;
	private String condition;
	private int pageSize;
	private String list;
	private String content;
	private String contente;
	private Page page;
	
	/**
	 * index:		批量删除文章
	 * @author guanhaimin
	 * @throws Exception 
	 * @date 2014年12月23日 下午2:47:08 void
	 */
	public void batchDeleteArticle() throws Exception {
		this.articleManagementService.batchDeleteArticle(list);
	}
	
	/**
	 * index:		删除文章
	 * @author guanhaimin
	 * @throws Exception 
	 * @date 2014年12月23日 下午2:06:17 void
	 */
	public void deleteArticle() throws Exception {
		this.articleManagementService.deleteArticle(id);
	}
	
	/**
	 * index:		文章管理
	 * @author guanhaimin
	 * @date 2014年12月23日 上午11:19:09
	 * @return String

	 * @throws JSONException 
	 */
	public String ArticleManagement() throws JSONException {
		this.articleManagementService.getArticle(currentPage, pageSize, condition);
		this.articleManagementService.getArticleCategorySort();
		return "ArticleManagement";
	}
	
	/**
	 * index:		添加修改文章
	 * @author guanhaimin
	 * @throws Exception 
	 * @date 2014年12月23日 上午9:40:38 void
	 */
	public String ArticleOperation() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		if(opt.trim().equals("NEW")){
			if(tbarticle != null){
				if(!content.trim().equals("")){
					this.articleManagementService.saveFile(content, opt, id);
				}
				if(!contente.trim().equals("")){
					this.articleManagementService.saveFile(contente, opt, id);
				}
				this.articleManagementService.saveArticle(tbarticle, content, contente);
			}else{
				this.articleManagementService.getArticleCategorySort();
			}
		}else{
			if(opt.trim().equals("EDIT")){
				if(tbarticle != null){
					if(!content.trim().equals("")){
						this.articleManagementService.saveFile(content, "EDITzh", tbarticle.getCId());
					}
					if(!contente.trim().equals("")){
						this.articleManagementService.saveFile(contente, "EDITen", tbarticle.getCId());
					}
					this.articleManagementService.updateArticle(tbarticle, content, contente);
				}else{
					this.articleManagementService.getArticleById(id);
					this.articleManagementService.getArticleCategorySort();
				}
			}
		}
		request.setAttribute("Opt", opt);
		return "ArticleOperation";
	}
	
	/**
	 * index:		指删除文章类别
	 * @author guanhaimin
	 * @throws Exception 
	 * @date 2014年12月22日 下午9:46:15 void
	 */
	public void batchDeleteArticleCategory() throws Exception {
		this.articleManagementService.batchDeleteArticleCategory(list);
	}
	
	/**
	 * index:		删除文章类别
	 * @author guanhaimin
	 * @throws Exception 
	 * @date 2014年12月22日 下午7:37:31 void
	 */
	public void deleteArticleCategory() throws Exception {
		this.articleManagementService.deleteArticleCategory(id);
	}
	
	/**
	 * index:		文章类别管理
	 * @author guanhaimin
	 * @date 2014年12月22日 上午9:36:33
	 * @return String
	 * @throws JSONException 
	 */
	public String ArticleCategoryManagement() throws JSONException {
		this.articleManagementService.getArticleCategory(currentPage, pageSize, condition);
		return "ArticleCategoryManagement";
	}
	
	/**
	 * index:		添加修改文章类别
	 * @author guanhaimin
	 * @throws Exception 
	 * @date 2014年12月20日 下午7:31:48 void
	 */
	public String ArticleCategoryOperation() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		if(opt.trim().equals("NEW")){
			if(carticlesort != null){
				this.articleManagementService.addArticleCategory(carticlesort);
			}
		}else{
			if(opt.trim().equals("EDIT")){
				if(carticlesort != null){
					this.articleManagementService.updateArticleCategory(carticlesort, carticlesort.getCId());
				}else{
					this.articleManagementService.getArticleCategoryById(id);
				}
			}
		}
		request.setAttribute("Opt", opt);
		return "ArticleCategoryOperation";
	}
	
	
	public ArticleManagementService getArticleManagementService() {
		return articleManagementService;
	}
	public void setArticleManagementService(
			ArticleManagementService articleManagementService) {
		this.articleManagementService = articleManagementService;
	}
	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	public String getOpt() {
		return opt;
	}
	public void setOpt(String opt) {
		this.opt = opt;
	}
	public CArticlesort getCarticlesort() {
		return carticlesort;
	}
	public void setCarticlesort(CArticlesort carticlesort) {
		this.carticlesort = carticlesort;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public String getCondition() {
		return condition;
	}

	public void setCondition(String condition) {
		this.condition = condition;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public String getList() {
		return list;
	}

	public void setList(String list) {
		this.list = list;
	}

	public TbArticle getTbarticle() {
		return tbarticle;
	}

	public void setTbarticle(TbArticle tbarticle) {
		this.tbarticle = tbarticle;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getContente() {
		return contente;
	}

	public void setContente(String contente) {
		this.contente = contente;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}
	

}
